package com.guli.mall.pms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.guli.common.valid.QueryGroup;
import com.guli.mall.pms.entity.AttrEntity;
import com.guli.mall.pms.entity.vo.AttrGroupAndAttrVo;
import com.guli.mall.pms.entity.vo.AttrGroupCategoryNameVo;
import com.guli.mall.pms.feign.ProductCategoryFeignClient;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import com.guli.mall.pms.entity.AttrGroupEntity;
import com.guli.mall.pms.service.AttrGroupService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author 罗俊华
 * @email 1007052116@qq.com
 * @date 2020-09-17 11:37:22
 */
@RestController
@RequestMapping("pms/attrgroup")
public class AttrGroupController {

    @Resource
    ProductCategoryFeignClient productCategoryFeignClient;

    @Autowired
    private AttrGroupService attrGroupService;


    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取attrGroup分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size) {
        PageUtils<AttrGroupCategoryNameVo> page = attrGroupService.getAttrGroupPaginationByCondition(currentPage, size, null);

        return R.ok().put("page", page);
    }


    @GetMapping("/info/{attrGroupId}")
    @ApiOperation("根据attrGroupId获取AttrGroup")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {

        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        List<Long> categoryIdPath = productCategoryFeignClient.findCategoryIdPath(attrGroup.getCatelogId());

        return R.ok().put("attrGroup", attrGroup).put("categoryIdPath", categoryIdPath);
    }

    @PostMapping("/save")
    @ApiOperation("插入AttrGroup")
    public R save(@RequestBody AttrGroupEntity attrGroup) {


        attrGroupService.save(attrGroup);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据attrGroupId更新AttrGroup")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据attrGroupId批量删除AttrGroup")
    public R delete(@RequestBody Long[] attrGroupIds) {

        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    @PostMapping("/getAttrGroupPaginationByCondition/{currentPage}/{size}")
    @ApiOperation("根据category条件查询AttrGroup")
    public R getAttrGroupPaginationByCondition(@RequestBody @Validated(QueryGroup.class) AttrGroupEntity attrGroupEntity,
                                               @PathVariable("currentPage") Integer currentPage,
                                               @PathVariable("size") Integer size) {

        PageUtils<AttrGroupCategoryNameVo> page = attrGroupService.getAttrGroupPaginationByCondition(currentPage, size, attrGroupEntity);

        return R.ok().put("page", page);
    }


    @PostMapping("/getAttrGroupAndItsAttrList/{catelogId}")
    @ApiOperation("通过catelogId获取到attrGroupList，再将与该attrGroup有关联的attr封装到本attrGroupList中")
    public R getAttrGroupAndItsAttrList(@PathVariable("catelogId") Long catelogId,
                                        @RequestBody AttrEntity attrEntity){

        List<AttrGroupAndAttrVo> list =  this.attrGroupService.getAttrGroupAndItsAttrList(catelogId,attrEntity);

        return R.ok().put("data",list);
    }

    @GetMapping("/querySpuInfoAttrValueAndFillIntoAttrGroup/{spuInfoId}")
    @ApiOperation("通过categoryId查询出该类型的spu能绑定的attrGroup和attr，再通过spuInfoId查询出其绑定的值，填充到刚刚的attr中，其中categoryId通过查询出来的skuInfo获得")
    public R querySpuInfoAttrValueAndFillIntoAttrGroup(@PathVariable("spuInfoId") Long spuInfoId){

        List<AttrGroupAndAttrVo> list = attrGroupService.querySpuInfoAttrValueAndFillIntoAttrGroup(spuInfoId);

        return R.ok().put("list",list);
    }
}
